#include "D3D9Engine.h"
#include "Helpers.h"


bool CD3D9Engine::RemoveResource(IResourceObject* objRemove, bool forceRelease)
{
	if(objRemove != NULL)
	{
		if(forceRelease)
			objRemove->OnFreeResource(this);

		this->Detach(objRemove);

		LogEntry(Helper::StrConcat("Resource removed! (%i)", objRemove->GetHash()) );
	}

	return false;
}


CResourceSprite* CD3D9Engine::CreateSprite(const char* fileName, int width, int height, DWORD alphaColor)
{
	CResourceSprite* sprite = NULL;

	if(this->_resFactory)
	{
		sprite = (CResourceSprite*)(this->_resFactory->CreateResource(CASGE_RES_SPRITE));

		if(sprite)
		{
			if(sprite->CreateSprite(fileName, width, height, alphaColor))
			{
				sprite->OnInitResource(this);
				this->Attach(sprite);

				LogEntry(Helper::StrConcat("CASGE_RES_SPRITE created! (%s) (%i)", fileName, sprite->GetHash()));
			}
		}
	}

	return sprite;
}


CResourceText* CD3D9Engine::CreateText(const char *font, int size, bool bold, bool italic, bool underline)
{
	CResourceText* text = NULL;

	if(this->_resFactory)
	{
		text = (CResourceText*)(this->_resFactory->CreateResource(CASGE_RES_TEXT));

		if(text)
		{
			if(text->CreateFont(font, size, bold, italic, underline))
			{
				text->OnInitResource(this);
				this->Attach(text);

				LogEntry(Helper::StrConcat("CASGE_RES_TEXT created! (%i)", font, text->GetHash()));
			}
		}
	}

	return text;
}


CResourceBitmapFont* CD3D9Engine::CreateBitmapFont(const char *fontTexture, int texWidth, int texHeight, int fontWidth, int fontHeight, DWORD colorKey, const char *_fontCSVFile)
{
	CResourceBitmapFont* bmpFont = NULL;

	if(this->_resFactory)
	{
		bmpFont = (CResourceBitmapFont*)(this->_resFactory->CreateResource(CASGE_RES_BMPFONT));

		if(bmpFont)
		{
			if(bmpFont->CreateBitmapFont(fontTexture, texWidth, texHeight, fontWidth, fontHeight, colorKey, _fontCSVFile))
			{
				bmpFont->OnInitResource(this);
				this->Attach(bmpFont);

				LogEntry(Helper::StrConcat("CASGE_RES_BMPFONT created! (%s) (%i)", fontTexture, bmpFont->GetHash()));
			}
		}

	}

	return bmpFont;
}


CResourceBitmapText* CD3D9Engine::CreateBitmapText(const char *text, CResourceBitmapFont *srcBitmapFont, DWORD color)
{
	CResourceBitmapText* bmpText = NULL;

	if(this->_resFactory)
	{
		bmpText = (CResourceBitmapText*)(this->_resFactory->CreateResource(CASGE_RES_BMPTEXT));

		if(bmpText)
		{
			if(bmpText->CreateBitmapText(text, srcBitmapFont, color))
			{
				bmpText->OnInitResource(this);
				this->Attach(bmpText);

				LogEntry(Helper::StrConcat("CASGE_RES_BMPTEXT created! (%i)", text, bmpText->GetHash()));
			}

		}

	}

	return bmpText;
}

CResourceSoundSource* CD3D9Engine::CreateSoundSource(const char *file, SND_FILETYPE fileType)
{
	CResourceSoundSource* sndSource = NULL;

	if (this->_resFactory)
	{
		sndSource = (CResourceSoundSource*) (this->_resFactory->CreateResource(CASGE_RES_SNDSOURCE));

		if (sndSource)
		{
			if (sndSource->CreateSoundSource(file, fileType))
			{
				sndSource->OnInitResource(this);
				this->Attach(sndSource);

				LogEntry(Helper::StrConcat("CASGE_RES_SNDSOURCE created!"));
			}
		}
	}

	return sndSource;
}